package personal.wang.juc.juc;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @className: AccumulatorWithSafe
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/3/21 17:49
 */
public class AccumulatorWithSafe {
    private int sum ;

    public int getSum() {
        return sum;
    }
    public static void main(String[] args) {
        AccumulatorWithSafe accumulatorWithSafe = new AccumulatorWithSafe();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.execute(()->{
            for (int i = 0; i < 100; i++) {
                accumulatorWithSafe.plus(1);
            }
        });

        executorService.execute(()->{
            for (int i = 0; i < 100; i++) {
                accumulatorWithSafe.plus(1);
            }
        });
        executorService.shutdown();

        System.out.println(accumulatorWithSafe.getSum());
    }
    public synchronized void plus(int i){
        sum = sum + i ;
    }
}
